var Writer = Class.create({
    initialize: function() {
        this.text = "";
        this.line = "";
        this.numTabs = 0;
    },
    indent: function() {
        this.numTabs++;
        return this;
    },
    outdent: function() {
        this.numTabs--;
        return this;
    },
    prefix: function() {
        var pre = "";
        var i;
        for (i = 0; i < this.numTabs; i++) {
            pre = pre + "\t";
        }
        return pre;
    },
    writePrefix: function() {
        if (this.line.length == 0) {
            this.line += this.prefix();
        }
        return this;
    },
    write: function(text) {
        this.writePrefix();
        this.line += text;
        return this;
    },
    writeLine: function(line) {
        return this.write(line).newLine();
    },
    newLine: function() {
        this.text += this.line + "\r\n";
        console.log("Added line: " + this.line);
        this.line = "";
        return this;
    }
});
var Composer = Class.create({
    initialize: function(name, options) {
        this.name = name;
        this.composers = [];
        this.options = this.defaults().merge(options);
    },
    addComposer: function(composer) {
        this.composers.push(composer);
        return this;
    },
    compose: function(writer) {
        this.error("compose function must be overridden");
    },
    compose_children: function(writer) {
        var shouldIndent = this.shouldIndent();
        if (shouldIndent) {
            writer.indent();
        }
        for (var index = 0, len = this.composers.length; index < len; ++index) {
            var composer = this.composers[index];
            composer.compose(writer);
        }
        if (shouldIndent) {
            writer.outdent();
        }
    },
    defaults: function() {
        return $H({text: this.name, indent: true});
    },
    error: function(msg) {
        throw this.name + ": " + msg;
    },
    shouldIndent: function() {
        return this.options.get('indent') === true;
    },
    validateNotNull: function(element, msg) {
        msg = msg ? msg : "validated element is null";
        if (element == null) {
            this.error(msg);
        }
    }
});

var CheckboxComposer = Class.create(Composer, {
    initialize: function($super, name, options) {
        $super(name, options.merge({indent: false}));
        this.checkbox = $(this.options.get('element_id'));
        this.validateNotNull(this.checkbox);
    },
    compose: function(writer) {
        console.log("composing " + this.name);
        if (this.checkbox.checked) {
            this.compose_children(writer);
        } else {
            console.log("skipping");
        }
    }
});
var DropdownNoSelectionComposer = Class.create(Composer, {
    initialize: function($super, name, options) {
        $super(name, options.merge({indent: false}));
        this.dropdown = $(this.options.get('element_id'));
        this.validateNotNull(this.dropdown);
    },
    compose: function(writer) {
        console.log("composing " + this.name);
        if (this.options.get('ignore_value') === this.dropdown.value) {
            console.log("value is '" + this.dropdown.value + "'; skipping");
        } else {
            this.compose_children(writer);
        }
    }
});
var DropdownComposer = Class.create(Composer, {
    initialize: function($super, name, options) {
        $super(name, options);
        this.dropdown = $(this.options.get('element_id'));
        this.validateNotNull(this.dropdown);
    },
    compose: function(writer) {
        console.log("composing " + this.name);
        writer.write(this.name + ": ").writeLine(this.dropdown.value);
        this.compose_children(writer);
    }
});
var MeetingComposer = Class.create(Composer, {
    initialize: function($super, button) {
        $super("Meeting", $H({indent: false}));
        this.button = $(button);
        this.validateNotNull(this.button);
        this.button.onclick = this.scheduleMeeting.bind(this);
    },
    scheduleMeeting: function() {
        console.log("scheduleMeeting called");
        var writer = new Writer();
        this.compose(writer);
        var eventData = { details: writer.text };
        google.calendar.composeEvent(eventData);
        console.log("scheduleMeeting complete");
    },
    compose: function(writer) {
        console.log("composing " + this.name);
        writer.writeLine("Connection Details:").newLine();
        this.compose_children(writer);
    }
});
var StaticTextComposer = Class.create(Composer, {
    initialize: function($super, name, options) {
        $super(name, options);
        this.static_text = this.options.get('text');
    },
    compose: function(writer) {
        console.log("composing " + this.name);
        writer.writeLine(this.static_text);
        this.compose_children(writer);
    }
});
var TextAreaComposer = Class.create(Composer, {
    initialize: function($super, name, options) {
        $super(name, options.merge({skip_when_empty: false}));
        this.text_area = $(this.options.get('element_id'));
        this.validateNotNull(this.text_area);
    },
    compose: function(writer) {
        console.log("composing " + this.name);
        if (!this.options.get('skip_when_empty') || this.text_area.value.trim.length > 0) {
            writer.writeLine(this.name + ": ").writeLine(this.text_area.value);
            this.compose_children(writer);
        }
    }
});
var TextComposer = Class.create(Composer, {
    initialize: function($super, name, options) {
        $super(name, options.merge({skip_when_empty: false}));
        this.text_input = $(this.options.get('element_id'));
        this.validateNotNull(this.text_input);
    },
    compose: function(writer) {
        console.log("composing " + this.name);
        if (!this.options.get('skip_when_empty') || this.text_input.value.trim.length > 0) {
            writer.write(this.name + ": ").writeLine(this.text_input.value);
            this.compose_children(writer);
        }
    }
});

var ElementToggler = Class.create({
    initialize: function(toggler_element, toggled_element) {
        this.toggler = $(toggler_element);
        this.toggled = $(toggled_element);
        this.toggler.onchange = this.toggle.bind(this);
    },
    toggle: function() {
        this.toggled.toggleClassName('hidden');
    }
});

function wire_readytalk() {
    var optionComposer = new CheckboxComposer("Readytalk Checkbox", $H({element_id: 'audio_readytalk'}));
    var rtc = new StaticTextComposer("ReadyTalk", $H({}));
    rtc.addComposer(new TextComposer("Conference Code", $H({element_id: 'readytalk_code'})));
    rtc.addComposer(new TextComposer("Leader PIN", $H({element_id: 'readytalk_pin', skip_when_empty: true})));
    optionComposer.addComposer(rtc);
    new ElementToggler('audio_readytalk', 'audio_readytalk_suboptions');
    return optionComposer;
}
function wire_lifesize() {
    var composer = new DropdownNoSelectionComposer("Lifesize Section", $H({
        element_id: 'select_lifesize',
        ignore_value: 'none'
    }));
    composer.addComposer(
            new DropdownComposer("LifeSize", $H({
                element_id: 'select_lifesize'
            })));
    return composer;
}
function wire_skype() {
    var composer = new StaticTextComposer("Skype");
    var sc1 = new DropdownNoSelectionComposer("Skype1 Section", $H({
        element_id: 'video_skype1',
        ignore_value: 'none'
    }));
    var sc2 = new DropdownNoSelectionComposer("Skype2 Section", $H({
        element_id: 'video_skype2',
        ignore_value: 'none'
    }));
    sc1.addComposer(new DropdownComposer("Skype (group 1)", $H({
        element_id: 'video_skype1'
    })));
    sc2.addComposer(new DropdownComposer("Skype (group 2)", $H({
        element_id: 'video_skype2'
    })));
    composer.addComposer(sc1);
    composer.addComposer(sc2);
    return composer;
}
function wire_screenshare() {
    var composer = new DropdownNoSelectionComposer("Screen Sharing", $H({
        element_id: 'collab_screen',
        ignore_value: 'none'
    }));
    composer.addComposer(
            new DropdownComposer("Screen Sharing", $H({
                element_id: 'collab_screen'
            })));
    return composer;
}
function wire_sharedocs() {
    var optionComposer = new CheckboxComposer("Shared Docs Checkbox", $H({element_id: 'collab_doc_link'}));
    var rtc = new TextAreaComposer("Shared Documents", $H({element_id: 'collab_doc_link_text'}));
    optionComposer.addComposer(rtc);
    new ElementToggler('collab_doc_link', 'collab_doc_link_suboptions');
    return optionComposer;
}
function wire_buddies() {
    var optionComposer = new CheckboxComposer("Buddies Checkbox", $H({element_id: 'collab_buddies'}));
    var rtc = new TextAreaComposer("Buddies", $H({element_id: 'collab_buddies_text'}));
    optionComposer.addComposer(rtc);
    new ElementToggler('collab_buddies', 'collab_buddies_suboptions');
    return optionComposer;
}
var init = function initialize() {
    var meetingComposer = new MeetingComposer('composeButton');
    meetingComposer.addComposer(wire_readytalk());
    meetingComposer.addComposer(wire_lifesize());
    meetingComposer.addComposer(wire_skype());
    meetingComposer.addComposer(wire_screenshare());
    meetingComposer.addComposer(wire_sharedocs());
    meetingComposer.addComposer(wire_buddies());
};
gadgets.util.registerOnLoadHandler(init);